package app.server.project.main.service.dto.rank;

import app.server.project.core.constants.Def;
import app.server.project.core.util.MathUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor(staticName = "of")
public class CommonRankDto {

    /**
     * 排行榜数值
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private long val;

    /**
     * 用户id
     */
    @JsonSerialize(using = ToStringSerializer.class)
    private long userId;
    /**
     * 昵称
     */
    private String nick;
    /**
     * 头像
     */
    private String headUrl;

    /**
     * 名次
     */
    private int pos;


    @JsonIgnore
    public void addVal(long val) {
        var ret = MathUtil.canAddNum(this.val, val);
        this.val += ret;
    }

    public void consumeVal(long val) {
        if (this.val >= val) {
            this.val -= val;
        } else {
            this.val = Def.Zero;
        }
    }
}
